<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="org.springframework.context.ApplicationContext" %>
<%@ page import="com.sz.common.ICommonDAO" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";


String style1 = "text-align:right;font-size: 12px";
String style2 = "width: 250px";
String style3 = "text-align:left;font-size: 12px;";

String region_id = request.getSession().getAttribute("regionid").toString();

ServletContext servletContext = request.getSession().getServletContext(); 
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
ICommonDAO dao = (ICommonDAO) context.getBean("CommonDAO");

//查到最顶级
StringBuffer buf = new StringBuffer();
buf.append("select * from region where region_level = 1 start with region_id = ");
buf.append(region_id);
buf.append(" connect by  prior parent_id =  region_id");
Map top = (Map)dao.queryForObject(buf.toString());

//自最顶级向下查
List list = new ArrayList();
if(top != null) {
	buf.delete(0, buf.length());
	buf.append("select * from region start with region_id = ");
	buf.append(top.get("REGION_ID").toString());
	buf.append(" connect by  parent_id = prior region_id");
	list = dao.queryForList(buf.toString());
}

String province_id = "";
String province_name = "";
for(int i = 0; i < list.size(); ++i) {
	Map map = (Map)list.get(i);
	if(map.get("REGION_LEVEL").toString().equals("1")) {
		province_id = map.get("REGION_ID").toString();
		province_name = map.get("REGION_NAME").toString();
		break;
	}
}

List city_list = new ArrayList();
for(int i = 0; i < list.size(); ++i) {
	Map map = (Map)list.get(i);
	if(map.get("REGION_LEVEL").toString().equals("2")) {
		city_list.add(map);
	}
}

List area_list = new ArrayList();
for(int i = 0; i < list.size(); ++i) {
	Map map = (Map)list.get(i);
	if(map.get("REGION_LEVEL").toString().equals("3")) {
		area_list.add(map);
	}
}

//根据登陆区域查商圈
buf.delete(0, buf.length());
buf.append("select * from business_group t where t.region_id in (");
buf.append("select region_id from region start with region_id = ");
buf.append(region_id);
buf.append(" connect by  parent_id = prior region_id)");
List group_list = dao.queryForList(buf.toString());

//菜系
buf.delete(0, buf.length());
buf.append("select serial_id, name from course_serial order by display_order");
List serial_list = dao.queryForList(buf.toString());
%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		
		<base href="<%=basePath%>">

		<title>新增订单-查询商家</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="addTicketMain">
		<meta http-equiv="description" content="新增订单-查询商家">
		
		<style type="text/css"> 
			@import "js/tableview/TableView.css";
    	</style>
	</head>
	
	<body>		
		<script type="text/javascript" src="js/jquery.js"></script>
		<script type="text/javascript" src="js/tableview/SortView.js"></script>
		<script type="text/javascript" src="js/tableview/PagerView.js"></script>
		<script type="text/javascript" src="js/tableview/TableView.js"></script>
		<script type="text/javascript" src="js/tableview/SelectorView.js"></script>
		<script type="text/javascript" src="js/tableview/PaperView.js"></script>
		<script type="text/javascript" src="js/utility.js"></script>
	
	  	<form name="business_query_form" style="width: 100%"> 
	  	 <table style="width: 100%" >	  	 
		  	<tr>
			  	<td style="<%=style1%>">行政区划</td>
			  	<td style="<%=style3%>">
			  	
			  		<select id="province" name="province">
			  		 <option value="">请选择</option>			  		
		             <option value="<%=province_id%>" selected="selected"> <%=province_name%> </option>		             			            
			  		</select>
			  		
			  		
			  		<select id="city" name="city">
			  		<option value="">请选择</option>
					 <%
					 String value;
					 String text;
					 boolean b =false;
					 for(int i = 0; i < city_list.size(); ++i) {
						Map map = (Map)city_list.get(i);
						value = map.get("REGION_ID").toString();
						text = map.get("REGION_NAME").toString();						
						if(region_id.equals(value)) b = true;  //与登陆区域相同，则选中
						if(b==false && i == city_list.size()-1) b = true;  //最后一个都还没有匹配，就默认选中
					 %>
					  <option value="<%=value%>"   <%if(b){%>selected="selected" <%}%> > 
					  <%=text%> 
					  </option>
					 <%}%> 
			  		</select>
			  		
			  	
			  		<select id="area" name="area">
			  		<option value="">请选择</option> 
			  		<%
					 for(int i = 0; i < area_list.size(); ++i) {
						Map map = (Map)area_list.get(i);
						value = map.get("REGION_ID").toString();
						text = map.get("REGION_NAME").toString();												
					 %>
					  <option value="<%=value%>"> 
					  <%=text%> 
					  </option>
					 <%} %> 
			  		</select>
			  	</td>
	
			  	
			  	<td style="<%=style1%>">商圈</td>
			  	<td>
			  		<select id="group_id" name="group_id"> 
				  		<option value="">请选择</option> 
				  		<%
						 for(int i = 0; i < group_list.size(); ++i) {
							Map map = (Map)group_list.get(i);
							value = map.get("GROUP_ID").toString();
							text = map.get("NAME").toString();												
						 %>
						  <option value="<%=value%>"> 
						  <%=text%> 
						  </option>
						 <%} %> 
			  		</select>
			  	</td>
			  	
			  	<td style="<%=style1%>">地标</td>
			  	<td>
			  		<input type="text" id="landmark" name="landmark"> 		  		
			  	</td>		  	
		  	</tr> 
		  	
		  	<tr>
		  	  <td style="<%=style1%>">菜系</td>
		  	  <td >
		  	      <select id="main_serial" name="main_serial" >
		  	      <option value="">请选择</option> 
			  		<%
					 for(int i = 0; i < serial_list.size(); ++i) {
						Map map = (Map)serial_list.get(i);
						value = map.get("SERIAL_ID").toString();
						text = map.get("NAME").toString();												
					 %>
					  <option value="<%=value%>"> 
					  <%=text%> 
					  </option>
					 <%} %> 
		  	      </select>
		  	  </td>
		  	  
		  	  <td style="<%=style1%>">风味</td>
		  	  <td >
		  	  	<input type="text" id="flavour" name="flavour">
		  	  </td>
		  	  
		  	  <td style="<%=style1%>">人均消费</td>
		  	  <td >
		  	  	<input type="text" id="avg_fee" name="avg_fee" >
		  	  </td>		  	  
		  	</tr>
		  	
		  	<tr>
		  	  <td style="<%=style1%>">名称</td>
		  	  <td>
		  	  	<input type="text" id="name" name="name" >
		  	  </td>
		  	  
		  	  <td style="<%=style1%>">地址</td>
		  	  <td>
		  	  	<input type="text" id="address" name="address" >
		  	  </td>
		  	  
		  	  <td style="<%=style1%>">商家编号</td>
		  	  <td>
		  	  	<input type="text" id="business_id" name="business_id" >
		  	  </td>
		  	  

		  	</tr>
		  	
		  	<tr>
		  	 <td style="<%=style1%>">首拼</td>
		  	  <td>
		  	  	<input type="text" id="py" name="py" >
		  	  </td>
		  	  
		  	  <td style="<%=style1%>">关键词</td>
		  	  <td>
		  	  	<input type="text" id="keywords" name="keywords" >
		  	  </td>
		  	  
		  	  <td colspan="4" style="<%=style1%>">
		  	  	<input type="checkbox" id="preorder_flag" value = "1" />可预订
		  	  	<input type="checkbox" id="creditcard_flag" value = "1" />信用卡
		  	  	<input type="checkbox" id="consumecard_flag" value = "1" />消费卡
		  	  	<input type="checkbox" id="park_flag" value = "1" />停车位
		  	  	<input type="checkbox" id="room_flag" value = "1" />包厢
		  	  	&nbsp&nbsp&nbsp&nbsp&nbsp
		  	  	<input type="button"  value="查询商家" onclick="onQueryBusiness()"> 
		  	  </td>
		  	</tr> 			
	  	</table>	  	
  	</form> 
  	

 	<table width="100%">
  		<tr>
	  		<td>
			  	<div id="table_div" style= "width: 100%"></div>
				<div id="pager"></div>
			</td>
		</tr>
  	</table>

  	
  	<SCRIPT LANGUAGE="JavaScript">
  	
  		var numPerPage = 10; //每页行数
		var paramValue = "";
		var methodCount = "queryBusinessCount";
		var methodPage = "queryBusinessPage";
		
		var table = new TableView('table_div');
		table.header = {
			BUSINESS_ID			: '商家编号',
			NAME				: '商家名称',
			REGION_FULLNAME		: '区域',
			ADDRESS				: '地址',
			TEL1				: '预订电话',
			SERIAL_NAME			: '主营菜系',
			AVG_FEE				: '人均消费',
			ORDER_DEMAND		: '预订要求',
			FULLSEAT			: '满座',
			CHECK_STATUS_STR	: '已签约',
			DETAIL				: '详细信息',
			OPERATE				: '操作'		
		};		
		table.customDefine = {
			BUSINESS_ID			: '',
			NAME				: '',
			REGION_FULLNAME		: 'width = "80px"',
			ADDRESS				: '',
			TEL1				: 'width = "160px"',
			SERIAL_NAME			: '',
			AVG_FEE				: '',
			ORDER_DEMAND		: 'width = "200px"',
			FULLSEAT			: 'width = "28px"',
			CHECK_STATUS_STR	: 'width = "40px"',
			DETAIL				: 'width = "60px"',
			OPERATE				: ''		
		};
				
		table.dataKey = 'BUSINESS_ID';
		table.count = false;
		table.title = false;
		table.display.filter = false;
		table.display.pager = false;
		table.display.multiple = false;
		table.pager.size = numPerPage;

		var pager = new PagerView('pager');
		pager.onclick = onPagerClick;
		pager.itemCount = 0;
		pager.size = numPerPage;
		
		table.render();
		pager.render();

		function queryBusiness(pageNO, returnCount) {
  	  		var begin = (pageNO-1) * numPerPage + 1;
  	  		var end = pageNO * numPerPage;
  	  		var mc = returnCount==true ? methodCount : "";
  	  		var date = {
  	  			BEAN_METHOD 	: "BusinessBMO.queryBusiness",
  	  			PARAM_CLASS 	: "java.util.Map",
  	    	  	PARAM_VALUE 	: paramValue,
  	    	  	METHOD_COUNT 	: mc, //为空则不返回记录数
  	    	  	METHOD_PAGE 	: methodPage, //为空则不分页
  	    	  	PAGE_BEGIN 		: begin, //仅在METHOD_PAGE不为空时使用
  	    	  	PAGE_END 		: end
  	  	    };
  	  		$.ajax({
				type: "POST",
				url: "query.exec",
				//data: {id:'123'},
				data: date,
				datatype:"json",
				success: query_success,
				error: show_error
  	  		});
  	  	}
  	  	
  	  	function onPagerClick(pageNO) {
  			queryBusiness(pageNO, "FLASE");
  		}
  	  	
  	  	//刷新当前页
  		function refreshCurrentPage() {
  			queryBusiness(pager.index, "FLASE");
  		}
  			
  		function onQueryBusiness() {
  			//先检查输入
  			var avg_fee =  document.getElementById("avg_fee").value;
  			avg_fee = jQuery.trim(avg_fee);
  			if(avg_fee != "" && IsInteger(avg_fee)==false) {
  				alert("请正确输入人均消费，只能为数字");
  				return;
  			}
  			
  			var region_id = "";
  			var province = document.getElementById("province").value;
  			var city = document.getElementById("city").value;
  			var area = document.getElementById("area").value;
  			
  			if(area != null && area != "") {
  				region_id = area;
  			} else if(city != null && city != "") {
  				region_id = city;
  			} else {
  				region_id = province;
  			}
  			
  			//默认取登陆的
  			if(region_id == null || region_id == "") {
  				region_id = "<%=region_id%>"
  			}
  			
  			//生成查询参数paramValue
  			var tmp;
			paramValue = "{";
			
			paramValue += " REGION_ID:'" + region_id;  //按目前的逻辑，region_id一直不为空
			paramValue += "'";
			paramValue += ",";
			
  			tmp = document.getElementById("group_id").value;
  			paramValue += " GROUP_ID:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
  			tmp = document.getElementById("main_serial").value;
  			paramValue += " MAIN_SERIAL:'" + tmp;
  			paramValue += "'";
			paramValue += ",";
			
  			tmp = document.getElementById("flavour").value;
  			tmp = jQuery.trim(tmp);
  			paramValue += " FLAVOUR:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("name").value;
			tmp = jQuery.trim(tmp);
  			paramValue += " NAME:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("address").value;
			tmp = jQuery.trim(tmp);
  			paramValue += " ADDRESS:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("business_id").value;
			tmp = jQuery.trim(tmp);
  			paramValue += " BUSINESS_ID:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("py").value;
			tmp = jQuery.trim(tmp).toUpperCase();
  			paramValue += " BUSINESS_CODE:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("keywords").value;
			tmp = jQuery.trim(tmp);
  			paramValue += " __KEYWORDS:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			//排序
			paramValue += "__ORDERBY:'";
			paramValue += "b.check_status desc";
			paramValue += "'";
			paramValue += ",";
			
			if(document.getElementById("preorder_flag").checked == true) {
				paramValue += " PREORDER_FLAG:'" + 1;
				paramValue += "'";
				paramValue += ",";
			}
			if(document.getElementById("creditcard_flag").checked == true) {
				paramValue += " CREDITCARD_FLAG:'" + 1;
				paramValue += "'";
				paramValue += ",";
			}
			if(document.getElementById("consumecard_flag").checked == true) {
				paramValue += " CONSUMECARD_FLAG:'" + 1;
				paramValue += "'";
				paramValue += ",";
			}
			if(document.getElementById("park_flag").checked == true) {
				paramValue += " PARK_FLAG:'" + 1;
				paramValue += "'";
				paramValue += ",";
			}
			if(document.getElementById("room_flag").checked == true) {
				paramValue += " ROOM_FLAG:'" + 1;
				paramValue += "'";
			}
  			  			
  			paramValue += "}";  			
  			
  			pager.index = 1;
  	  		queryBusiness(1, true);
  		}
  		
  		function query_success(data) {
  	  		var jdata = eval("(" + data + ")");
  	  		if(jdata.COUNT != null) { //有记录数返回  	  	  		
  	  	  		pager.itemCount = jdata.COUNT;
  	  	  		pager.size = numPerPage;  	  	  		
  	  	  		pager.render();
  	  		} 
		   var array = eval(jdata.LIST);
		   for(var i = 0; i < array.length; ++i) {
		   		//形如：<a href="javascript:" onclick="about()">modal</a>
		   		var url = "<a href=\"javascript:\" onclick=\"onBusinessDetail('";
		   		url += array[i].BUSINESS_ID;
		   		url += "')\">详细信息</a>";
		   		array[i].DETAIL = url;
		   		
		   		url = "<input type=\"button\" value=\"短信\" onclick=\"onSMS('";
		   		url += array[i].BUSINESS_ID;		  
		   		url += "')\" />";		   		
		   		url += "<input type=\"button\" value=\"预订\" onclick=\"onOrder('";
		   		url += array[i].BUSINESS_ID;		  
		   		url += "')\" />";
		   		array[i].OPERATE = url;
		   		
		   		//商家推荐
		   		var rc = array[i].RECOMMAND_COUNT;		   				   		
		   		url = array[i].NAME;
		   		if(rc != "0") {
			   		url += "<a href=\"javascript:\" style=\"color:red;text-decoration:none\" onclick=\"onBusinessRecomand('";
			   		url += array[i].BUSINESS_ID;
			   		url += "')\">&nbsp;荐</a>";
		   		}
		   		array[i].NAME = url;
		   		
		   		//满座
		   		url = "";
		   		var c = array[i].FULLSEAT_COUNT;
		   		if(c != "0") {
		   			url += "<font style=\"color:red;\">" + "满" + "</font>";
		   		}
		   		array[i].FULLSEAT = url;
		   		
		   		//签约
		   		url = "";
		   		var cs = array[i].CHECK_STATUS;
		   		var check_str = "";
		   		if(cs == "1") {
		   			check_str = "是";
		   		} else {
		   			check_str = "否";
		   		}
		   		url += "<font style=\"color:red;\">" + check_str + "</font>";
		   		array[i].CHECK_STATUS_STR = url;
		   }
		   table.clear();
		   table.addRange(array);
		}
  		function show_error(data) {
  			var err = "操作失败！\n";
  			err += "status：" + data.status + "\n";
  			err += "statusText：" + data.statusText;
  	  		alert(err);
 		}
 		
 		function onBusinessDetail(business_id) {
 			var url = "<%=path%>";
  			url += "/page/viewBusiness.jsp?business_id=" + business_id;
  			var param = "status:no;scroll:yes;dialogWidth:1024px;dialogHeight:600px";
			//window.showModalDialog(url, window, param);
			
			var width = 1024; //弹出窗口的宽度;
			var height = 600; //弹出窗口的高度;
			var top = (window.screen.availHeight-30-height)/2; //获得窗口的垂直位置;
			var left = (window.screen.availWidth-10-width)/2; //获得窗口的水平位置;
			var style = "toolbar=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no";			
			window.open (url,"商家信息","height="+height+",width="+width+",top="+top+",left="+left+","+style);
			 
 		}
 		
 		function onOrder(business_id) {
 			url = "<%=path%>";
  			url += "/ticket/addTicket.page?business_id=" + business_id;  			
  			var param = "status:no;scroll:no;dialogWidth:1024px;dialogHeight:600px";
			//window.showModalDialog(url, window, param);
			
			var width = 1024; //弹出窗口的宽度;
			var height = 660; //弹出窗口的高度;
			var top = (window.screen.availHeight-30-height)/2; //获得窗口的垂直位置;
			var left = (window.screen.availWidth-10-width)/2; //获得窗口的水平位置;
			var style = "toolbar=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no";			
			window.open (url,"订单生成","height="+height+",width="+width+",top="+top+",left="+left+","+style);			
 		}
  	
  		function onBusinessRecomand(business_id) {
  			var url = "<%=path%>";
  			url += "/page/businessRecommand.jsp?primary_business_id=" + business_id;
  			  		
  			var width = 1200; //弹出窗口的宽度;
			var height = 600; //弹出窗口的高度;
			var top = (window.screen.availHeight-30-height)/2; //获得窗口的垂直位置;
			var left = (window.screen.availWidth-10-width)/2; //获得窗口的水平位置;
			var style = "toolbar=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no";			
			window.open (url,"商家推荐","height="+height+",width="+width+",top="+top+",left="+left+","+style);
  		}
  		
  		function callback_businessRecommand(business_id) {
  			onOrder(business_id);
  		}
  		
  		function onSMS(business_id) { 			
 			var url = "<%=path%>";
  			url += "/page/businessSMS.jsp?business_id=" + business_id;  			
  			
  			var width = 600; //弹出窗口的宽度;
			var height = 320; //弹出窗口的高度;
			var top = (window.screen.availHeight-30-height)/2; //获得窗口的垂直位置;
			var left = (window.screen.availWidth-10-width)/2; //获得窗口的水平位置;
			var style = "toolbar=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no";			
			window.open (url,"短信","height="+height+",width="+width+",top="+top+",left="+left+","+style); 			
 		}
  	</SCRIPT>
	</body>
</html>
